package com.chj.comp.service.demo1.controller;

import com.chj.comp.common.core.util.Result;
import com.chj.comp.common.es.document.AccessLogDocument;
import com.chj.comp.common.es.document.PersonDataDocument;
import com.chj.comp.service.demo1.service.IEsTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuihui
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/es")
@Api(tags = "es测试接口")
@Slf4j
public class EsTestController {

    @Autowired
    private IEsTestService esTestService;

    /**
     * 保存访问日志至es（按月）
     */
    @PostMapping("/saveAccessLog")
    @ApiOperation(value = "保存访问日志至es（按月）")
    public Result saveAccessLog(@RequestBody AccessLogDocument accessLogDocument) {
        accessLogDocument.setId(UUID.randomUUID().toString());
        if(StringUtils.isEmpty(accessLogDocument.getCreateTime())){
            accessLogDocument.setCreateTime("2022-06-04 16:09:31");
        }
        try {
            esTestService.saveAccessLog(accessLogDocument);
            return Result.ok("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("保存失败");
        }
    }

    /**
     * 根据id获取访问日志
     */
    @PostMapping("/getAccessLogById/{id}")
    @ApiOperation(value = "根据id获取访问日志")
    public Result<AccessLogDocument> getAccessLogById(@PathVariable String id){
        return esTestService.getAccessLogById(id);
    }

    /**
     * 保存人员数据至es（单索引）
     */
    @PostMapping("/savePersonData")
    @ApiOperation(value = "保存人员数据至es（单索引）")
    public Result savePersonData(@RequestBody PersonDataDocument personDataDocument) {
        personDataDocument.setId(UUID.randomUUID().toString());
        if(StringUtils.isEmpty(personDataDocument.getCreateTime())){
            personDataDocument.setCreateTime("2022-06-04 16:09:31");
        }
        try {
            esTestService.savePersonData(personDataDocument);
            return Result.ok("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("保存失败");
        }
    }
}
